import { message } from 'antd';
import { getDistrictListByBussiness, getWorkOrderList, getWorkOrderDetail, checkAndAssign, orderComplete } from '../services/api';

export default {
  namespace: 'immanage',

  state: {
    data: {
      list: [],
      pagination: {},
    },
    workOrderInfo: {},
    workOrderList: [],
    districtList: [],
    networkSpeedList: [],
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(getWorkOrderList, payload);
      yield put({
        type: 'workOrderLists',
        payload: response,
      });
    },
    *getWorkOrderDetail({ payload }, { call, put }) {
      const response = yield call(getWorkOrderDetail, payload);
      yield put({
        type: 'workOrderInfos',
        payload: response,
      });
    },
    *districtList({ payload }, { call, put }) {
      const response = yield call(getDistrictListByBussiness, payload);
      yield put({
        type: 'districtList',
        payload: response,
      });
    },
    *checkAndAssign({ payload, callback }, { call }) {
      const response = yield call(checkAndAssign, payload);
      if (response.code === 0) {
        if (callback) callback();
      } else {
      }
    },
    *orderComplete({ payload, callback }, { call }) {
      const response = yield call(orderComplete, payload);
      if (response.code === 0) {
      } else {
      }
      if (callback) callback();
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },

    districtList(state, action) {
      return {
        ...state,
        districtList: action.payload.data,
      };
    },
    workOrderLists(state, action) {
      return {
        ...state,
        workOrderList: action.payload.data,
      };
    },
    workOrderInfos(state, action) {
      return {
        ...state,
        workOrderInfo: action.payload.data || {},
      };
    },
  },
};
